﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FantasyBlog.Models;
using FantasyBlog.DAL;
using System.Web.Security;
using System.Data;

namespace FantasyBlog.Controllers
{
    public class UserController : Controller
    {
        UserModel model = new UserModel();

        public ActionResult Profile()
        {
            var tempTable = model.db.UserSet.Where(u => u.username == User.Identity.Name);
            User user = tempTable.ToList().FirstOrDefault();
            //Roles.AddUsersToRole(new string[] { "123" }, "admin");

            return View(user);
        }

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Roles="admin")]
        public ActionResult Profile(FormCollection formCollection)
        {
            var tempTable = model.db.UserSet.Where(u => u.username == User.Identity.Name);
            User user = tempTable.ToList().FirstOrDefault();
            if (formCollection["pwd"] != null) user.password = formCollection["pwd"];
            if (formCollection["email"] != null) user.email = formCollection["email"];
            model.db.SaveChanges();
            return RedirectToAction("Index");
            //if (ModelState.IsValid)
            //{
            //    model.db.Entry(user).State = EntityState.Modified;
            //    model.db.SaveChanges();
            //    return RedirectToAction("Index");
            //}
            //return View(user);
        }

        //
        // GET: /User/

        [Authorize(Roles = "admin")]
        public ActionResult Index(int page = 1, int pageSize = 5, string UserName = "", string UserStatus = "", string sortBy = "ID", bool ascending = true)
        {
            model.CurrentPage = page;
            model.PageSize = pageSize;
            model.UserName = UserName;
            model.UserStatus = UserStatus;
            model.SortBy = sortBy;
            model.SortAscending = ascending;
            // 获取当前页的信息
            model.getUsers();
            return View(model);
        }

        //
        // GET: /AdminUser/Details/5

        [Authorize(Roles = "admin")]
        public ViewResult Details(int id)
        {
            User user = model.db.UserSet.Find(id);
            return View(user);
        }

        //
        // GET: /AdminUser/Create

        [Authorize(Roles = "admin")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AdminUser/Create

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                model.db.UserSet.Add(user);
                model.db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(user);
        }

        //
        // GET: /AdminUser/Edit/5

        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            User user = model.db.UserSet.Find(id);
            return View(user);
        }

        //
        // POST: /AdminUser/Edit/5

        //[HttpPost]
        //[Authorize(Roles = "admin")]
        //public ActionResult Edit(User user)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        model.db.Entry(user).State = EntityState.Modified;
        //        model.db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    return View(user);
        //}

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Roles = "admin")]
        public ActionResult Edit(FormCollection formCollection)
        {
            string userid = formCollection["userid"];
            User u = model.db.UserSet.Find(int.Parse(userid));
            u.displayname = formCollection["displayname"];
            u.email = formCollection["email"];
            u.registered = DateTime.Parse(formCollection["registered"]);
            u.status = formCollection["status"];
            model.db.SaveChanges();
            return RedirectToAction("Index");
        }

        [Authorize(Roles = "admin")]
        public ActionResult EditRole(int id)
        {
            UserWithRolesModel ur = new UserWithRolesModel(id);
            return View(ur);
        }

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Roles = "admin")]
        public ActionResult EditRole(FormCollection formCollection)
        {
            string username = formCollection["username"];
            User u = model.db.UserSet.Find(int.Parse(username));
            u.Roles.Clear();
            model.db.SaveChanges();
            string[] userArr={u.username};
            List<string> roleArr = new List<string>();
            var uwr = new UserWithRolesModel(u.ID);
            foreach (var item in uwr.AllRoles)
            {
                if (formCollection[item.name.Trim()] != null)
                {
                    roleArr.Add(item.name);
                }
            }
            uwr.rp.AddUsersToRoles(userArr, roleArr.ToArray());
            return RedirectToAction("Index");
        }

        //
        // GET: /AdminUser/Delete/5

        [Authorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            User user = model.db.UserSet.Find(id);
            return View(user);
        }

        //
        // POST: /AdminUser/Delete/5

        [HttpPost, ActionName("Delete")]
        [Authorize(Roles = "admin")]
        public ActionResult DeleteConfirmed(int id)
        {
            User user = model.db.UserSet.Find(id);
            model.db.UserSet.Remove(user);
            model.db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            model.db.Dispose();
            base.Dispose(disposing);
        }
    }
}
